﻿$import( "SmitearWebGL.Core.ResourceItem" );

SmitearWebGL.Resources.Buffer = SmitearWebGL.Core.ResourceItem.extend(
{
	/******** constructor ********/

	init : function()
	{
		this._super();
		this._registClass( "SmitearWebGL.Resources.Buffer" );

		/******** public property ********/


		/******** private property ********/

		this._glBuffer = null;
		this._glData = null;
		this._arrayType = null;
		this._bindType = null;
		this._elementDescriptions = null;
	}
	,

	/******** public method ********/
	
	getGlBuffer : function()
	{
		return this._glBuffer;
	}
	,
	getBindType : function()
	{
		return this._bindType;
	}
	,
	setElementDescriptions : function( descriptions )
	{
		this._elementDescriptions = descriptions;
	}
	,
	getElementDescriptions : function()
	{
		return this._elementDescriptions;
	}
	,
	bind : function()
	{
		$THROW( this.isResourceCreated(), "该资源还没有被创建." );
		
		this.context.gl.bindBuffer( this._bindType , this._glBuffer );
		
		return SmitearWebGL.Core.HResult.SUCCESS;
	}
	,
	bindData : function( data )
	{
		$THROW( this.isResourceCreated(), "该资源还没有被创建." );
		
		this._glData = new this._arrayType( data );
		this.unlock();
	}
	,
	lock : function( numElements )
	{
		$THROW( this.isResourceCreated(), "该资源还没有被创建." );
		
		this._glData = new this._arrayType( numElements );
		
		return this._glData;
	}
	,
	unlock : function()
	{
		$THROW( this.isResourceCreated(), "该资源还没有被创建." );
		
		this.context.gl.bindBuffer( this._bindType, this._glBuffer );
		this.context.gl.bufferData( this._bindType, this._glData, this.context.gl.STATIC_DRAW );
	}
	,

	/******** private method ********/

	/******** inherit method ********/

	_createResource : function()
	{
		this._super();
		
		this._glBuffer = this.context.gl.createBuffer();
		this._glData = null;
		this._arrayType = Float32Array;
		this._bindType = this.context.gl.ARRAY_BUFFER;
		this._elementDescriptions = null;
	}
	,
	_destroyResource : function()
	{
		this._super();
		
		this.context.gl.deleteBuffer( this._glBuffer );

		this.itemSize = 0;
		this.numItems = 0;
		this.size = 0;

//		this._glData.slice( 0, this._glData.length );
		this._glData = null;
		this._glBuffer = null;
		this._elementDescriptions = null;
	}
}
);

SmitearWebGL.Resources.Buffer.AttribLocation =
{
	//
	POSITION		: "aPosition",
	NORMAL			: "aNormal",
	INDEX			: "index",
	TEXCOORD		: "aTexcoord",
	FRUSTUMCOORDS	: "aFrustumCoords",
	
	//terrain
	POSITION0		: "aPosition0",
	POSITION1		: "aPosition1",
}

SmitearWebGL.Resources.Buffer.DataType =
{
	BYTE :				0,
	UNSIGNED_BYTE :		1,
	FLOAT :				2,
	SHORT :				3,
	UNSIGNED_SHORT :	4
}

SmitearWebGL.Resources.Buffer.getGlDataType = function( gl, dataType )
{
	switch ( dataType )
	{
		case SmitearWebGL.Resources.Buffer.DataType.BYTE :
			return gl.BYTE;
		case SmitearWebGL.Resources.Buffer.DataType.UNSIGNED_BYTE :
			return gl.UNSIGNED_BYTE;
		case SmitearWebGL.Resources.Buffer.DataType.FLOAT :
			return gl.FLOAT;
		case SmitearWebGL.Resources.Buffer.DataType.SHORT :
			return gl.SHORT;
		case SmitearWebGL.Resources.Buffer.DataType.UNSIGNED_SHORT :
			return gl.UNSIGNED_SHORT;
	}
}